package com.css.fxfzxczh.modules.synthesizeInfoManage.houseStructureManagement.service.impl;

import com.css.fxfzxczh.constants.YNEnum;
import com.css.fxfzxczh.modules.samplingPointDataCensus.param.QueryParam;
import com.css.fxfzxczh.modules.synthesizeInfoManage.houseStructureManagement.entity.HouseStructureManagement;
import com.css.fxfzxczh.modules.synthesizeInfoManage.houseStructureManagement.repository.HouseStructureManagementRepository;
import com.css.fxfzxczh.modules.synthesizeInfoManage.houseStructureManagement.service.HouseStructureManagementService;
import com.css.fxfzxczh.util.CreateGson;
import com.css.fxfzxczh.util.PlatformDateUtils;
import com.css.fxfzxczh.web.PlatformSessionContext;
import com.css.fxfzxczh.zcpt.sys.entity.SUser;
import com.css.fxfzxczh.zcpt.sys.service.SUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/01/12
 * @Description: TODO
 */
@Service
public class HouseStructureManagementServiceImpl implements HouseStructureManagementService {

    @Resource
    private HouseStructureManagementRepository houseStructureManagementRepository;

    @Resource
    SUserService sUserService;

    @Override
    public void save(HouseStructureManagement param) {
        param.setXhstmId(UUID.randomUUID().toString().replaceAll("-", ""));
        param.setDelFlag(YNEnum.N.toString());
        String userID = PlatformSessionContext.getUserID(); // 获取当前登录用户
        param.setCreateUser(userID);
        param.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        houseStructureManagementRepository.save(param);
    }

    @Override
    public void updateById(HouseStructureManagement param) {
        String userID = PlatformSessionContext.getUserID(); // 获取当前登录用户
        param.setUpdateUser(userID);
        param.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        houseStructureManagementRepository.updateById(param);
    }

    @Override
    public void deleteById(String id) {
        houseStructureManagementRepository.deleteById(id);
    }

    @Override
    public Map<String, Object> getPage(HouseStructureManagement queParams, int curPage, int pageSize) {
        Map<String, Object> page = houseStructureManagementRepository.getPage(queParams, curPage, pageSize);
        return page;
    }

    //获取用户信息
    @Override
    public HouseStructureManagement getHeadInfo() {
        HouseStructureManagement houseStructureManagement = new HouseStructureManagement();
        String userId = PlatformSessionContext.getUserID();
        String user = sUserService.getSUser(userId);
        SUser sUser = CreateGson.createGson().fromJson(user, SUser.class);
        houseStructureManagement.setCreateUser(sUser.getUserName());
        houseStructureManagement.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        houseStructureManagement.setCompany(sUser.getOrgName());
        return houseStructureManagement;
    }

    @Override
    public HouseStructureManagement findById(String id) {
        HouseStructureManagement byId = houseStructureManagementRepository.findById(id);
        String user = sUserService.getSUser(byId.getCreateUser());
        SUser sUser = CreateGson.createGson().fromJson(user, SUser.class);
        byId.setCreateUser(sUser.getUserName());
        return byId;
    }

    @Override
    public List<HouseStructureManagement> findAll() {
        return houseStructureManagementRepository.findAll();
    }

    @Override
    public List<String> getPreStrType(String category) {
        return houseStructureManagementRepository.getPreStrType(category);
    }


}
